package com.qinggeng.project.system.service.impl;

import java.util.List;
import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.project.api.dto.JobInfoBrowseUser;
import com.qinggeng.project.system.domain.JobInfoCollect;
import com.qinggeng.project.system.domain.JobUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qinggeng.project.system.mapper.JobInfoBrowseMapper;
import com.qinggeng.project.system.domain.JobInfoBrowse;
import com.qinggeng.project.system.service.IJobInfoBrowseService;

import javax.annotation.Resource;

/**
 * 招聘信息浏览记录Service业务层处理
 *
 * @author smallfish
 * @date 2020-12-24
 */
@Slf4j
@Service
public class JobInfoBrowseServiceImpl implements IJobInfoBrowseService
{
    @Resource
    private JobInfoBrowseMapper jobInfoBrowseMapper;

    /**
     * 查询招聘信息浏览记录
     *
     * @param browseId 招聘信息浏览记录ID
     * @return 招聘信息浏览记录
     */
    @Override
    public JobInfoBrowse selectJobInfoBrowseById(Long browseId)
    {
        return jobInfoBrowseMapper.selectJobInfoBrowseById(browseId);
    }

    /**
     * 查询招聘信息浏览记录列表
     *
     * @param jobInfoBrowse 招聘信息浏览记录
     * @return 招聘信息浏览记录
     */
    @Override
    public List<JobInfoBrowse> selectJobInfoBrowseList(JobInfoBrowse jobInfoBrowse)
    {
        return jobInfoBrowseMapper.selectJobInfoBrowseList(jobInfoBrowse);
    }

    /**
     * 新增招聘信息浏览记录
     *
     * @param jobInfoBrowse 招聘信息浏览记录
     * @return 结果
     */
    @Override
    public boolean insertJobInfoBrowse(JobInfoBrowse jobInfoBrowse)
    {
        jobInfoBrowse.setCreateTime(DateUtils.getNowDate());
        return jobInfoBrowseMapper.insertJobInfoBrowse(jobInfoBrowse)>0;
    }

    /**
     * 修改招聘信息浏览记录
     *
     * @param jobInfoBrowse 招聘信息浏览记录
     * @return 结果
     */
    @Override
    public int updateJobInfoBrowse(JobInfoBrowse jobInfoBrowse)
    {
        jobInfoBrowse.setUpdateTime(DateUtils.getNowDate());
        return jobInfoBrowseMapper.updateJobInfoBrowse(jobInfoBrowse);
    }

    /**
     * 批量删除招聘信息浏览记录
     *
     * @param browseIds 需要删除的招聘信息浏览记录ID
     * @return 结果
     */
    @Override
    public int deleteJobInfoBrowseByIds(Long[] browseIds)
    {
        return jobInfoBrowseMapper.deleteJobInfoBrowseByIds(browseIds);
    }

    /**
     * 删除招聘信息浏览记录信息
     *
     * @param browseId 招聘信息浏览记录ID
     * @return 结果
     */
    @Override
    public int deleteJobInfoBrowseById(Long browseId)
    {
        return jobInfoBrowseMapper.deleteJobInfoBrowseById(browseId);
    }

    public boolean updateJobInfoBrowse(Long jobUserId,Long jobInfoId,String browseType){

        try{
            JobInfoBrowse jobInfoBrowse=new JobInfoBrowse();
            jobInfoBrowse.setJobUserId(jobUserId);
            jobInfoBrowse.setJobInfoId(jobInfoId);
            jobInfoBrowse.setBrowseType(browseType);
            JobInfoBrowse oldBrowse=jobInfoBrowseMapper.getJobInfoBrowseByJobUserIdAndJobInfoIdAndBrowseType(jobInfoBrowse);
            if(oldBrowse!=null){
                return this.updateJobInfoBrowseCount(oldBrowse);
            }
            jobInfoBrowse.setBrowseCount(1);
            return this.insertJobInfoBrowse(jobInfoBrowse);
        }catch (Exception e){
            System.out.println(e.getMessage());
            log.error(e.getMessage());
            return false;
        }
    }

    @Override
    public List<JobInfoBrowseUser> listJobInfoBrowseUserByJobInfoIdAndBrowseType(Long jobInfoId, String browseType) {
        JobInfoBrowse jobInfoBrowse=new JobInfoBrowse();
        jobInfoBrowse.setJobInfoId(jobInfoId);
        jobInfoBrowse.setBrowseType(browseType);
        return jobInfoBrowseMapper.listJobInfoBrowseUserByJobInfoIdAndBrowseType(jobInfoBrowse);
    }

    public boolean updateJobInfoBrowseCount(JobInfoBrowse jobInfoBrowse){
        jobInfoBrowse.setUpdateTime(DateUtils.getNowDate());
        return jobInfoBrowseMapper.updateJobInfoBrowseCount(jobInfoBrowse)>0;
    }



}
